"""

1通过列表推导式生成学生数据。重构原数据（姓名、数学、物理、化学成绩）。
2用生成器表达式计算每个学生的平均分（保留两位小数），并添加到字典中。
3找出数学成绩最高的学生。
4使用字典推导式创建“科目-成绩映射”。
5使用集合操作找出所有学生都参加的科目。
students = [
    {"name": "Alice", "math": 85, "physics": 90, "chemistry": 78},
    {"name": "Bob", "math": 76, "physics": 88, "chemistry": 92},
    {"name": "Charlie", "math": 92, "physics": 85, "chemistry": 88},
    {"name": "David", "math": 88, "physics": 95, "chemistry": 85}
]
"""
# 通过列表推导式生成学生数据。
students = [{"name": name,
             "math": 85 if name == "Alice" else (76 if name == "Bob" else (92 if name == "Charlie" else 88)),
             "physics": 90 if name == "Alice" else (88 if name == "Bob" else (85 if name == "Charlie" else 95)),
             "chemistry": 78 if name == "Alice" else (92 if name == "Bob" else (88 if name == "Charlie" else 85)), }
            for name in ["Bob", "Charlie", "David", "Alice"]]
for x in students:
    print(x)
# 2用生成器表达式计算每个学生的平均分（保留两位小数），并添加到字典中。
avg_stu = {x["name"]:
               round(sum((score for score in x.values() if isinstance(score, int))) / 3, 2)
           for x in students}
print(avg_stu)
# 3找出数学成绩最高的学生。
max_stu = [x["name"] for x in students if x["math"] == max([x["math"] for x in students])]
print(max_stu)
# 4使用字典推导式创建“科目-成绩映射”。这个不会
subject_score_map = {}
# 先初始化字典
for student in students:
    for subject in student:
        if subject != "name":
            subject_score_map.setdefault(subject, []).append(student[subject])

# 再转换为字典推导式（需要借助临时变量）
subject_score_map = {
    subject: scores
    for subject, scores in subject_score_map.items()
}

print(subject_score_map)
# 5使用集合操作找出所有学生都参加的科目。
